home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 136 (1990-05-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 136 (1990-05-15)(Ossowski, Stefan)(DE)(PD).adf / ScreenZap / screenzap.c < prev    next >
C/C++ Source or Header  |  1990-02-23  |  7KB  |  246 lines

  1. /*
  2.  *               ScreenZap    Version 2.3
  3.  *                  by Lars R. Clausen
  4.  *     Hack this code as you want to, as long as I get the credit for
  5.  *                      ScreenZap
  6.  */
  7.  
  8. #include <intuition/intuitionbase.h>
  9. #include <graphics/gfxbase.h>
  10. #include <exec/ports.h>
  11. #include <exec/memory.h>
  12.  
  13. void *OpenWindow();
  14. void *GetMsg();
  15. void *OpenLibrary();
  16. void *AllocMem();
  17.  
  18. struct Window *myw;
  19. struct IntuitionBase *IntuitionBase;
  20. struct GfxBase *GfxBase;
  21.  
  22. SHORT Pairs1[10][2] =
  23. {{2,2}, {0,4}, {0,19}, {4,23}, {305,23}, {309,19}, {309,4}, {305,0}, {4,0},
  24. {2,2}};
  25.  
  26. SHORT Pairs2[5][2]={{3,2}, {3,21}, {306,21}, {306,2}, {3,2}};
  27.  
  28. struct Border GadBord2 =
  29. { 0,0,3,1,JAM1,10,&Pairs1[0][0],NULL};
  30.  
  31. struct Border GadBord1 =
  32. { 0,0,1,2,JAM1,5,&Pairs2[0][0],&GadBord2};
  33.  
  34. struct IntuiText GadText[4] =
  35. {{3,2,JAM2,38,8,NULL,(UBYTE *)" Kill Screens Behind WorkBench ",NULL},
  36.  {3,2,JAM2,38,8,NULL,(UBYTE *)" Kill Screens Except WorkBench ",NULL},
  37.  {3,2,JAM2,86,8,NULL,(UBYTE *)" Kill Front Screen ",NULL},
  38.  {3,2,JAM2,14,8,NULL,(UBYTE *)" Kill Front Window On Front Screen ",NULL}
  39. };
  40.  
  41. struct Gadget Gad[4]=
  42. {
  43.     { &Gad[1], 7, 12, 310, 24, GADGHCOMP, RELVERIFY | GADGIMMEDIATE ,
  44.     BOOLGADGET, (APTR) &GadBord1, NULL, &GadText[0], NULL, NULL, 0, NULL},
  45.     { &Gad[2], 7, 39, 310, 24, GADGHCOMP, RELVERIFY | GADGIMMEDIATE ,
  46.     BOOLGADGET, (APTR) &GadBord1, NULL, &GadText[1], NULL, NULL, 1, NULL},
  47.     { &Gad[3], 323, 12, 310, 24, GADGHCOMP, RELVERIFY | GADGIMMEDIATE ,
  48.     BOOLGADGET, (APTR) &GadBord1, NULL, &GadText[2], NULL, NULL, 2, NULL},
  49.     { NULL, 323, 39, 310, 24, GADGHCOMP, RELVERIFY | GADGIMMEDIATE ,
  50.     BOOLGADGET, (APTR) &GadBord1, NULL, &GadText[3], NULL, NULL, 3, NULL}
  51. };
  52.  
  53. struct NewWindow nw =
  54. {
  55.     0,10,640,66,0,1,CLOSEWINDOW | GADGETUP,
  56.     WINDOWCLOSE | ACTIVATE | WINDOWDEPTH | WINDOWDRAG | SMART_REFRESH | NOCAREREFRESH,
  57.     &Gad[0], NULL,(UBYTE *)"Click To Zap Window Or Screen",
  58.     NULL, NULL,295,50,295,50,WBENCHSCREEN
  59. };
  60.  
  61. /* Remove and reply any IntuiMessages hanging off of a port
  62.  * which were addressed to a particular window.
  63.  * Note that technique does not rely on ln_Succ of a message
  64.  * after it has been replied.
  65.  */
  66. void StripIntuiMessages( mp, win )
  67. struct MsgPort *mp;
  68. struct Window *win;
  69. {
  70.     struct IntuiMessage *msg;
  71.     struct Node *succ;
  72.  
  73.     msg = (struct IntuiMessage *) mp->mp_MsgList.lh_Head;
  74.  
  75.     while( succ = msg->ExecMessage.mn_Node.ln_Succ ) {
  76.  
  77.     if( msg->IDCMPWindow ==  win ) {
  78.         /* Intuition is about to rudely free this message.
  79.          * Make sure that we have politely sent it back.
  80.          */
  81.         Remove( (struct Node *) msg );
  82.  
  83.         ReplyMsg( (struct Message *) msg );
  84.     }
  85.  
  86.     msg = (struct IntuiMessage *) succ;
  87.     }
  88. }
  89.  
  90. void CloseWindowSafely( win )
  91. struct Window *win;
  92. {
  93.      /* we forbid here to keep out of race conditions with Intuition */
  94.     /* Forbid(); */
  95.     
  96.      /* send back any messages for this window 
  97.       * that have not yet been processed
  98.       */
  99.      StripIntuiMessages( win->UserPort, win );
  100.  
  101.      /* clear UserPort so Intuition will not free it */
  102.      win->UserPort = NULL;
  103.  
  104.      /* tell inuition to stop sending more messages */
  105.      ModifyIDCMP( win, 0L );
  106.  
  107.      /* turn tasking back on */
  108.     /* Permit(); */
  109.      /* Inserted by L. Clausen to get out menus; Remove it to get to original
  110.         routine */
  111.     if (win->MenuStrip!=NULL) ClearMenuStrip(win);
  112.  
  113.      /* and really close the window */
  114.      CloseWindow( win );
  115. }
  116.  
  117. /* bail out! */
  118. abort(nr)
  119. int nr;
  120. {
  121.     CloseWindow(myw);
  122.     CloseLibrary(IntuitionBase);
  123.     CloseLibrary(GfxBase);
  124.     exit(nr);
  125. }
  126.  
  127. /* open everything needed */
  128. void OpenIt()
  129. {
  130.     IntuitionBase = OpenLibrary("intuition.library",0L);
  131.     if (IntuitionBase == 0)
  132.     {
  133.        exit(1);
  134.     }
  135.     GfxBase = OpenLibrary("graphics.library",0L);
  136.     if (GfxBase == 0)
  137.     {
  138.        abort(1);
  139.     }
  140.     myw = OpenWindow(&nw);
  141.     if (myw == 0)
  142.     {
  143.        abort(2);
  144.     }
  145.     SetWindowTitles(myw,"Click To Zap Window Or Screen","ScreenZapper V2.3 (c) 1989 Lars R. Clausen. This Program is PD.");
  146. }
  147.  
  148. /* Zap screen - nr 0: All behind workbench, nr 1: All except workbench,
  149.    nr 2: Front screen.
  150.     This is not multitasking-friendly, but I need the system for myself. */
  151. ScreenZap(nr)
  152. int nr;
  153. {
  154.     struct Screen *Curs,*Nexts;
  155.     struct Window *Curw;
  156.     int x,y=0;
  157.     char *Title;
  158.  
  159.     Forbid();
  160.     if (nr==0) Curs = myw->WScreen;
  161.     else Curs = IntuitionBase->FirstScreen;
  162.     if (Curs==myw->WScreen) Curs=Curs->NextScreen;
  163.     for (x=0; Curs!=NULL; x++)
  164.     {
  165.         if (Curs->FirstWindow!=NULL)  /* first close all windows */
  166.         {
  167.             for (Curw=Curs->FirstWindow;Curw->NextWindow!=NULL;Curw=Curs->FirstWindow)
  168.             {
  169.                 for (;Curw->NextWindow!=NULL; Curw=Curw->NextWindow);
  170.                 CloseWindowSafely(Curw);
  171.                 y++;
  172.             }
  173.             CloseWindowSafely(Curw);
  174.             y++;
  175.         }
  176.         Nexts = Curs->NextScreen;
  177.         CloseScreen(Curs);
  178.         if (Nexts==myw->WScreen) Nexts=Nexts->NextScreen;
  179.         Curs = Nexts;
  180.         if (nr==2) Curs=NULL;
  181.     }
  182.     Permit();
  183.     if ((x>1) && (y>1)) sprintf(&Title,"%d Screens, %d Windows Zapped.",x,y);
  184.     if ((x>1) && (y==1)) sprintf(&Title,"%d Screens, 1 Window Zapped.",x);
  185.     if ((x==1) && (y>1)) sprintf(&Title,"1 Screen, %d Windows Zapped.",y);
  186.     if ((x==1) && (y==1)) Title="1 Screen & 1 Window Zapped.";
  187.     if ((x>1) && (y==0)) sprintf(&Title,"%d Screens Zapped.",x);
  188.     if ((x==1) && (y==0)) Title = "1 Screen Zapped.";
  189.     if (x==0) Title = "Nothing Zapped";
  190.     SetWindowTitles(myw,Title,"ScreenZapper V2.3 (c) 1989 Lars R. Clausen. This Program is PD.");
  191. }
  192.  
  193. /* To kill the front window */
  194. void WindowZap()
  195. {
  196.     struct Screen *Curs;
  197.     struct Window *Curw;
  198.     struct IntuiMessage *UserMsg;
  199.     
  200.     Forbid();
  201.     Curs = IntuitionBase->FirstScreen;
  202.     Curw = (struct Window *)Curs->LayerInfo.top_layer->Window;
  203.     if (Curw)
  204.     {
  205.         if (Curw==myw)
  206.         {
  207.             SetWindowTitles(myw,"That's ME! Use CloseGadget to exit, anything else to continue.",
  208.                              "ScreenZapper V2.3 (c) 1989 Lars R. Clausen. This Program is PD.");
  209.             WaitPort(myw->UserPort);
  210.             UserMsg=GetMsg(myw->UserPort);
  211.             if (UserMsg->Class==CLOSEWINDOW) abort(0);
  212.             else
  213.             SetWindowTitles(myw,"Thanks! Maybe the window has gone - try a Screen kill.",
  214.                              "ScreenZapper V2.3 (c) 1989 Lars R. Clausen. This Program is PD.");
  215.         }
  216.         else
  217.         {
  218.             CloseWindowSafely(Curw);
  219.             SetWindowTitles(myw,"Window is now zapped, ready again",
  220.                                  "ScreenZapper V2.3 (c) 1989 Lars R. Clausen. This Program is PD.");
  221.         }
  222.     }
  223.     Permit();
  224. }
  225.  
  226. _main()
  227. {
  228.     struct IntuiMessage *msg;
  229.     struct Gadget *gad;
  230.     int nr;
  231.  
  232.     OpenIt();
  233.     WaitPort(myw->UserPort);
  234.     for (msg=GetMsg(myw->UserPort);msg->Class == GADGETUP;msg=GetMsg(myw->UserPort))
  235.     {
  236.         gad=(struct Gadget *)msg->IAddress;
  237.         nr=gad->GadgetID;
  238.         ReplyMsg(msg);
  239.         if (nr<3) ScreenZap(nr);
  240.         else WindowZap();
  241.         WaitPort(myw->UserPort);
  242.     }
  243.     ReplyMsg(msg);
  244.     abort(0);
  245. }
  246.